home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / Scheduler.h < prev    next >
C/C++ Source or Header  |  1990-05-19  |  5KB  |  144 lines

  1. #ifndef    SCHEDULER_H
  2. #define    SCHEDULER_H
  3.  
  4. /*$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Scheduler.h,v 3.0 90/05/20 00:21:06 kgorlen Rel $*/
  5.  
  6. /* Scheduler.h -- declarations for the Process Scheduler
  7.  
  8.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  9.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  10.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  11.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  12.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  13.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  14.  
  15. Author:
  16.     K. E. Gorlen
  17.     Computer Systems Laboratory, DCRT
  18.     National Institutes of Health
  19.     Bethesda, MD 20892
  20.  
  21. $Log:    Scheduler.h,v $
  22.  * Revision 3.0  90/05/20  00:21:06  kgorlen
  23.  * Release for 1st edition.
  24.  * 
  25. */
  26. #include "LinkedList.h"
  27. #include "StackProc.h"
  28. #include "nihclconfig.h"
  29. #ifdef HAVE_SELECT
  30. #include <sys/time.h>
  31. #endif
  32.  
  33. class HeapProc;
  34.  
  35. class Scheduler : public NIHCL {
  36. public:            // type definitions
  37.     enum setjmp_val { schedule_process=0, resume_current_process, resume_new_process };
  38. public:            // static member functions
  39.     static unsigned char activePriority()    { return active_process->priority(); }
  40.     static Process& activeProcess()        { return *active_process; }
  41.     static bool astActive()            { return ast_level != 0; }
  42.     static const char* className();
  43.     static void dumpOn(ostream& strm =cerr);
  44. #ifndef NESTED_TYPES
  45.     static void initialize(stackTy* bottom, int priority =0);
  46. #else
  47.     static void initialize(Process::stackTy* bottom, int priority =0);
  48. #endif
  49.     static void printOn(ostream& strm =cout);
  50.     static void terminateActive()        { activeProcess().terminate(); }
  51.     static void schedule();
  52.     static void yield();
  53. public:            // static member variables
  54.     static JMP_BUF switcher;    // environment for stack switcher
  55. // The following must be public so they can be accessed from the MAIN_PROCESS macro
  56.     static Process* active_process;
  57.     static StackProc* main_stack_process;
  58. #ifndef NESTED_TYPES
  59.     static stackTy* main_stack_bottom;
  60. #else
  61.     static Process::stackTy* main_stack_bottom;
  62. #endif
  63. private:        // static member variables
  64.     static Process* previous_process;
  65.     static unsigned runCount;    // total # of runnable processes 
  66.     static LinkedList runList[MAXPRIORITY+1];
  67. #ifdef HAVE_SELECT
  68.     static LinkedList selectList;    // Process waiting for select
  69.     static struct timeval selectTimeout;    // timeout on select
  70.     static int selectfd();            // do select(2) system call
  71.     void timeOutSelect()    { selectTimeout.tv_sec = selectTimeout.tv_usec = 0; }
  72. #endif
  73. protected:        // static member variables
  74.     static int ast_level;    // AST nesting level 
  75.     Scheduler() {}        // to prevent construction of instances
  76. private:        // member functions
  77.     Scheduler(const Scheduler&) {}    // to prevent copy of instances
  78.     static void addProcess(Process& p);
  79.     friend Process;
  80.     friend StackProc::StackProc(stackTy* bottom, int priority);
  81.     friend void StackProc::switchFrom(HeapProc*);
  82.     friend void StackProc::switchFrom(StackProc*);
  83. };
  84.  
  85. #ifndef NESTED_TYPES
  86. inline void Scheduler::initialize(stackTy* bottom, int priority)
  87. #else
  88. inline void Scheduler::initialize(Process::stackTy* bottom, int priority)
  89. #endif
  90. {
  91.     Scheduler::main_stack_bottom = bottom;
  92. // Create MAIN process
  93.     Scheduler::active_process = new StackProc(bottom,priority);
  94. }
  95.  
  96. /*
  97. main() must call MAIN_PROCESS first thing to initialize the scheduler.
  98.  
  99. Note: MAIN_PROCESS must avoid using blocks in code executed as the
  100. result of a longjmp() because such blocks may contain
  101. compiler-generated local variables that share storage with local
  102. variables used elsewhere in main().  Executing such a block after a
  103. longjmp() would cause these local variables to be overwritten.  Inline
  104. functions must be avoided for the same reason.
  105.  
  106. */
  107.  
  108. #ifndef NESTED_TYPES
  109. #define _STACKTY stackTy
  110. #else
  111. #define _STACKTY Process::stackTy
  112. #endif
  113.  
  114. #if STACK_GROWS_DOWN
  115.  
  116. #define _STACKPROC_ALLOCATE(active) \
  117.     alloca((Scheduler::main_stack_bottom - (active)->stack_top) * sizeof(_STACKTY)) \
  118.  
  119. #else
  120.  
  121. #define _STACKPROC_ALLOCATE(active) \
  122.     alloca(((active)->stack_top - Scheduler::main_stack_bottom) * sizeof(_STACKTY)) \
  123.  
  124. #endif
  125.  
  126. #define MAIN_PROCESS(priority) \
  127.     if (_SETJMP(Scheduler::switcher) == 0) goto _begin_main_process; \
  128. /* longjmp() here to restore a process's stack */ \
  129.     ((StackProc*)Scheduler::active_process)-> \
  130.         restoreStack((_STACKTY*)_STACKPROC_ALLOCATE((StackProc*)Scheduler::active_process)); \
  131. _begin_main_process: \
  132.     Scheduler::initialize((_STACKTY*)alloca(sizeof(_STACKTY)),priority); \
  133.  
  134. class AST_LEVEL : public Scheduler {
  135. public:
  136.     AST_LEVEL()    { ast_level++; }
  137.     ~AST_LEVEL()    { ast_level--; }
  138. };
  139.  
  140. // AST/signal handlers must begin with an AST_ENTER
  141. #define AST_ENTER    AST_LEVEL ast_level_dummy
  142.  
  143. #endif
  144.